package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.CrmConsumerBinder;
import com.xbongbong.pro.rabbitmq.binders.DistributorConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.CrmBinderConstant;
import com.xbongbong.pro.rabbitmq.constant.DistributorBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.CrmFeignClient;
import com.xbongbong.pro.rabbitmq.feign.DistributorFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * distributor 消息消费者
 * @author 魏荣杰
 * @date 2019/2/25 14:04
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(DistributorConsumerBinder.class)
public class DistributorListener {
    private static Logger logger = LoggerFactory.getLogger(DistributorListener.class);

    @Resource
    private DistributorFeignClient distributorFeignClient;

    @StreamListener(value = DistributorBinderConstant.DISTRIBUTOR_INPUT)
    public void distributorReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_ADD_PUSH:
                    response = distributorFeignClient.distributorAdd(message);
                    break;
                case DISTRIBUTOR_TEAM_ADD_DEL_MAIN:
                    response = distributorFeignClient.distributorTeamMain(message);
                    break;
                case DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC:
                    response = distributorFeignClient.distributorTeamSynergetic(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("DistributorListener.distributorReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("DistributorListener.distributorReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = DistributorBinderConstant.DISTRIBUTOR_HANDOVER_INPUT)
    public void distributorHandoverReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_HANDOVER_PUSH:
                    response = distributorFeignClient.distributorHandover(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("DistributorListener.distributorHandoverReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("DistributorListener.distributorHandoverReceive Exception, type=" + type, e);
        }

    }

    @StreamListener(value = DistributorBinderConstant.DISTRIBUTOR_CONTACT_INPUT)
    public void distributorContactReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_CONTACT_ADD_PUSH:
                    response = distributorFeignClient.distributorContactAdd(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("DistributorListener.distributorContactReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("DistributorListener.distributorContactReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = DistributorBinderConstant.DISTRIBUTOR_COMMUNICATE_INPUT)
    public void distributorCommunicateReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_COMMUNICATE_ADD_PUSH:
                    response = distributorFeignClient.distributorCommunicateAdd(message);
                    break;
                case DISTRIBUTOR_COMMUNICATE_LIKE_PUSH:
                    response = distributorFeignClient.distributorCommunicateLike(message);
                    break;
                case DISTRIBUTOR_COMMUNICATE_COMMENT_PUSH:
                    response = distributorFeignClient.distributorCommunicateComment(message);
                    break;
                case DISTRIBUTOR_COMMUNICATE_REPLY_PUSH:
                    response = distributorFeignClient.distributorCommunicateReplay(message);
                    break;
                case DISTRIBUTOR_COMMUNICATE_AT_PUSH:
                    response = distributorFeignClient.distributorCommunicateAt(message);
                    break;
                case DISTRIBUTOR_COMMUNICATE_COMMENT_DEL_PUSH:
                    response = distributorFeignClient.distributorCommunicateCommentDel(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("DistributorListener.distributorCommunicateReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("DistributorListener.distributorCommunicateReceive Exception, type=" + type, e);
        }
    }

}
